﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using ERP_RORZE.Tool.UserControls;
using ERP_RORZE_TOOL.BizLogic;
using ERP_RORZE_TOOL.Common;
using ERP_RORZE_TOOL.Common.ValueObject;

namespace ERP_RORZE.Tool.Forms
{
	public partial class frm_ReturnEdit : DevExpress.XtraEditors.XtraForm
	{
		public int Id;
		public DateTime Date;
		public string ToolId;
		public int StatusId;
		public int Quantity;
		public string Staff;
		public string Machine;
		public int ToolOrQTool;
		public string Note;

	    private int number;

		private DataTable dt = new DataTable();
		private ucReturn parent;

		public frm_ReturnEdit(ucReturn fReturn)
		{
			InitializeComponent();
			parent = fReturn;
			this.CancelButton = this.btTCancel;
		}

		private void rdTool_CheckedChanged(object sender, EventArgs e)
		{
			BindDataTool(ToolConstants.Configuration.ToolType.IsTool);
			ToolOrQTool = ToolConstants.Configuration.ToolType.IsTool;
			// "0" : Tool
			// "1" : QTool
		}

		private void rdQTool_CheckedChanged(object sender, EventArgs e)
		{
			BindDataTool(ToolConstants.Configuration.ToolType.IsQTool);
			ToolOrQTool = ToolConstants.Configuration.ToolType.IsQTool;
		}

		private void XtraForm1_Load(object sender, EventArgs e)
		{
			dtpTDate.Value = Date;
			txtTQuantity.Text = Quantity.ToString();
			memoTNote.Text = Note;
			BindDataStaff();
			BindDataMachine();
			BindDataStatus();
			if (ToolOrQTool == 0)
			{
				rdTool.Checked = true;
			}
			else
			{
				rdQTool.Checked = true;
			}

			cmbToolID.SelectedValue = ToolId;
			if(cmbToolID.SelectedItem == null)
			{
				cmbToolID.SelectedItem = cmbToolID.Items[0];
			}

		}

		private void btTUpdate_Click_1(object sender, EventArgs e)
		{
			UpdateData();
		}

		private void btTCancel_Click_1(object sender, EventArgs e)
		{
			this.Close();
		}

        private void cmbToolID_SelectionChangeCommitted(object sender, EventArgs e)
        {
            if (cmbToolID.SelectedItem != cmbToolID.Items[0])
            {
                string fieldName = ToolConstants.StoreProcedure.FieldName.Total;
                string where = ToolConstants.StoreProcedure.FieldName.MachineID + "='" + cmbMachine.SelectedValue + "' and " + ToolConstants.StoreProcedure.FieldName.ToolID + "='" + cmbToolID.SelectedValue + "'";
                string storedName = ToolConstants.StoreProcedure.QTotalMachineAtFactoryBorrow;
                string tempTableName = ToolConstants.StoreProcedure.TemporaryTables.QTotalMachineAtFactoryBorrow;

                number = Utilities.CheckInput(fieldName, storedName, where, true, tempTableName);
                if (number == 0)
                    MessageBox.Show(string.Format(ToolConstants.Message.Return.MachineNotBorrow, cmbMachine.SelectedValue, cmbToolID.SelectedValue), ToolConstants.Form.MessageBox.Warning, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                else
                {
                    MessageBox.Show(string.Format(ToolConstants.Message.Return.MachineBorrow, cmbMachine.SelectedValue, number.ToString()), ToolConstants.Form.MessageBox.Warning, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
            }
        }


		#region Private methods

		private bool GetDataTool(Return o)
		{
			#region Check valid

			if(Utilities.IsNullOrEmpty(dtpTDate.Value.ToString()))
			{
				MessageBox.Show(ToolConstants.Message.Return.DateCantEmpty, ToolConstants.Form.MessageBox.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
				return false;
			}

			if(Utilities.IsNullOrEmpty(cmbToolID.SelectedValue.ToString()))
			{
				MessageBox.Show(ToolConstants.Message.Return.ToolIDCantEmpty, ToolConstants.Form.MessageBox.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
				return false;
			}

			if(Utilities.IsNullOrEmpty(cmbStatus.SelectedValue.ToString()))
			{
				MessageBox.Show(ToolConstants.Message.Return.StatusCantEmpty, ToolConstants.Form.MessageBox.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
				return false;
			}

			if(Utilities.IsNullOrEmpty(cmbStaff.SelectedValue.ToString()))
			{
				MessageBox.Show(ToolConstants.Message.Return.StaffCantEmpty, ToolConstants.Form.MessageBox.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
				return false;
			}


			if(Utilities.IsNullOrEmpty(cmbMachine.SelectedValue.ToString()))
			{
				MessageBox.Show(ToolConstants.Message.Return.MachineCantEmpty, ToolConstants.Form.MessageBox.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
				return false;
			}


			if(Utilities.IsNullOrEmpty(txtTQuantity.Text.Trim()))
			{
				MessageBox.Show(ToolConstants.Message.Return.QuantityCantEmpty, ToolConstants.Form.MessageBox.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
				return false;
			}
            if (int.Parse(txtTQuantity.Text) <= 0)
            {
                MessageBox.Show(ToolConstants.Message.Return.QuantityMustGreaterZero, ToolConstants.Form.MessageBox.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
                txtTQuantity.Focus();
                return false;

            }

            if (int.Parse(txtTQuantity.Text) > 0)
            {

                string fieldName = ToolConstants.StoreProcedure.FieldName.Total;
                string where = ToolConstants.StoreProcedure.FieldName.MachineID + "='" + cmbMachine.SelectedValue + "' and " + ToolConstants.StoreProcedure.FieldName.ToolID + "='" + cmbToolID.SelectedValue + "'";
                string storedName = ToolConstants.StoreProcedure.QTotalMachineAtFactoryBorrow;
                string tempTableName = ToolConstants.StoreProcedure.TemporaryTables.QTotalMachineAtFactoryBorrow;

                number = Utilities.CheckInput(fieldName, storedName, where, true, tempTableName);

                if (number < int.Parse(txtTQuantity.Text))
                {
                    MessageBox.Show(string.Format(ToolConstants.Message.Return.WrongQuantity, txtTQuantity.Text, number.ToString()), ToolConstants.Form.MessageBox.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
                    txtTQuantity.Focus();
                    return false;
                }
                else if (number > int.Parse(txtTQuantity.Text))
                {
                    MessageBox.Show(string.Format(ToolConstants.Message.Return.ResultQuantity, cmbMachine.SelectedValue, number.ToString(), cmbToolID.SelectedValue, txtTQuantity.Text, number - int.Parse(txtTQuantity.Text)), ToolConstants.Form.MessageBox.Message, MessageBoxButtons.OK, MessageBoxIcon.Information);
                }

            }
			#endregion

			o.ID = Id;
			o.Date = dtpTDate.Value;
			o.ToolID = cmbToolID.SelectedValue.ToString();
			o.StatusID = int.Parse(cmbStatus.SelectedValue.ToString());
			o.StaffID = cmbStatus.SelectedValue.ToString();
			o.Quantity = int.Parse(txtTQuantity.Text);
			o.StaffID = cmbStaff.SelectedValue.ToString();
			o.MachineID = cmbMachine.SelectedValue.ToString();
			o.ToolOrQTool = ToolOrQTool;
			o.Note = memoTNote.Text;

			return true;
		}

		private void BindDataTool(int type)
		{
			ToolHelper helper = new ToolHelper(new Tools());
			helper.ValueObject.ToolOrQTool = type;
			dt = helper.GetAllByToolType();
			DataRow row = dt.NewRow();

			cmbToolID.DataSource = dt;
			cmbToolID.DisplayMember = ToolConstants.Entities.Tool.FieldName.FullName;
			cmbToolID.ValueMember = ToolConstants.Entities.Tool.FieldName.ToolID;

            row[ToolConstants.Entities.Tool.FieldName.FullName] = "Select...";
			row[ToolConstants.Entities.Tool.FieldName.ToolID] = null;
			dt.Rows.InsertAt(row, 0);

			cmbToolID.SelectedItem = cmbToolID.Items[0];
		}

		private void BindDataStaff()
		{
			StaffHelper helper = new StaffHelper(new Staff());
			DataTable dt = helper.GetAll();
			DataRow row = dt.NewRow();

			cmbStaff.DataSource = dt;
            cmbStaff.DisplayMember = ToolConstants.Entities.Staff.FieldName.FullName;
			cmbStaff.ValueMember = ToolConstants.Entities.Staff.FieldName.StaffID;
            row[ToolConstants.Entities.Staff.FieldName.FullName] = "Select...";
			row[ToolConstants.Entities.Staff.FieldName.StaffID] = null;
			dt.Rows.InsertAt(row, 0);
			cmbStaff.SelectedValue = Staff;

			if(cmbStaff.SelectedItem == null)
			{
				cmbStaff.SelectedItem = cmbStaff.Items[0];
			}
		}

		private void BindDataMachine()
		{
			MachineHelper helper = new MachineHelper(new Machine());
			DataTable dt = helper.GetAll();
			DataRow row = dt.NewRow();

			cmbMachine.DataSource = dt;
            cmbMachine.DisplayMember = ToolConstants.Entities.Machine.FieldName.FullName;
			cmbMachine.ValueMember = ToolConstants.Entities.Machine.FieldName.MachineID;
            row[ToolConstants.Entities.Machine.FieldName.FullName] = "Select...";
			row[ToolConstants.Entities.Machine.FieldName.MachineID] = null;
			dt.Rows.InsertAt(row, 0);
			cmbMachine.SelectedValue = Machine;
			if(cmbMachine.SelectedItem == null)
			{
				cmbMachine.SelectedItem = cmbMachine.Items[0];
			}
		}

		private void BindDataStatus()
		{
			ToolStatusHelper helper = new ToolStatusHelper(new ToolStatus());
			DataTable dt = helper.GetAll();
			DataRow row = dt.NewRow();

			cmbStatus.DataSource = dt;
			cmbStatus.DisplayMember = ToolConstants.Entities.ToolStatus.FieldName.StatusName;
			cmbStatus.ValueMember = ToolConstants.Entities.ToolStatus.FieldName.StatusID;
			row[ToolConstants.Entities.ToolStatus.FieldName.StatusName] = "Select...";
			row[ToolConstants.Entities.ToolStatus.FieldName.StatusID] = DBNull.Value;
			dt.Rows.InsertAt(row, 0);

			cmbStatus.SelectedValue = StatusId.ToString();
			if(cmbStatus.SelectedItem == null)
			{
				cmbStatus.SelectedItem = cmbStatus.Items[0];
			}
		}

		private void UpdateData()
		{
			ReturnHelper helper = new ReturnHelper(new Return());
			if(GetDataTool(helper.ValueObject))
			{

				Utilities.Timer timer = new Utilities.Timer();

				if(helper.Update() > 0)
				{
					timer.lb= lblStatus;
					timer.ShowHideLabel(lblStatus, 3000, Color.Blue, ToolConstants.Message.UpdateSuccess);
				}
				else
				{
					timer.lb= lblStatus;
					timer.ShowHideLabel(lblStatus, 3000, Color.Red, ToolConstants.Message.UpdateFailed);
				}
				parent.BindDatatoGrid();

			}
		}

		#endregion


		



	}
}